package com.rf.sys.jstl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.rf.sys.model.Dictionary;
import com.rf.sys.service.DictionaryService;
import com.rf.sys.utils.StringUtils;
/**
 * 定义单选下拉列表
 * 
 * @author Administrator
 * 
 */
public class SelectTag extends SimpleTagSupport {
	@Resource
	private DictionaryService dictionaryService;
	
	// 参数编码
	private String paramCode;
	// 选中值
	private String selectedValue;
	// 不包含参数 默认为空
	private String exceptValue;
	// 名称
	private String name;
	// 是否只读  默认为空
	private String readOnly;
	// 是否有请选择  默认为false
	private String pleaseChoose;
	// change事件
	private String onchange;
	// 是否为多选  默认为false单选
	private String multiSelect;
	// 是否为必选项 默认为false
	private String required;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();
		try {
			if (StringUtils.isNotBlank(this.paramCode)) {
				setSpringService();
				List<String> exceptDataCode = new ArrayList<String>();
				//判断不包含参数是否有内容
				if(this.exceptValue != null && !this.exceptValue.equals("")){
					String[] ev = exceptValue.split(",");
					//循环处理不包含的内容
					for(int i=0;i<ev.length;i++){
						exceptDataCode.add(ev[i]);
					}
				}
				String classCode = this.paramCode;
				//查询下拉选项
				List<Dictionary> dList = dictionaryService.getDictionaryByDatakey(classCode, exceptDataCode);
				//如果为只读则处理为只读输入框
				if ("readOnly".equals(this.readOnly)){
					for (Dictionary dictionary : dList) {
						if (dictionary.getDataCode().equals(this.selectedValue)) {
							out.print("<input class=\"easyui-textbox\" id=\"" + this.name
									+ "\" name=\"" + this.name
									+ "\" readonly=\"readonly\" value=\""
									+ dictionary.getDataName() + "\"/>");
						}
					}
				} else {
					out.print("<select class=\"easyui-combobox\" name=\""
							+ this.name + "\" id=\"" + this.name + "\"" +" style=\"width: 155px\" ");
					
					if("true".equals(multiSelect) || "true".equals(required)){
						out.print(" data-options=\" ");
						//判断是否可以多选
						if ("true".equals(multiSelect)) {
							out.print(" multiple:true ");
							//判断是否为必选项
							if ("true".equals(required)) {
								out.print(",required:true ");
							}
						}else{
							//必有一项存在
							out.print(" required:true ");
						}
						out.print(" \" ");
					}
					//是否有onchange方法
					if (!StringUtils.isNullString(onchange)) {
						out.print(" onchange=\"" + this.onchange + "\"");
					}
					out.print(" >");//输出结尾符
					//判断是否出现请选择***
					if(pleaseChoose!=null && !pleaseChoose.equals("")){
						if(pleaseChoose.equals("true")){
							out.print("<option value=''>请选择</option>");
						}else{
							out.print("<option value=''>请选择"+pleaseChoose+"</option>");
						}
					}
					//循环输出选项
					for (Dictionary dictionary : dList) {
						//初始化值和选项相同则为选中项
						if (dictionary.getDataCode().equals(this.selectedValue)) {
							out.print("<option value='" + dictionary.getDataCode()
									+ "' selected='selected'>"
									+ dictionary.getDataName() + "</option>");
						} else {
							out.print("<option value='" + dictionary.getDataCode()
									+ "'>" + dictionary.getDataName() + "</option>");
						}
					}
					out.print("</select>");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.doTag();
	}

	public void setSpringService() {
		PageContext pageContext = (PageContext) this.getJspContext();
		ServletContext servletContext = pageContext.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		this.dictionaryService = (DictionaryService) wac.getBean(DictionaryService.class);
	}
	

	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}

	public void setPleaseChoose(String pleaseChoose) {
		this.pleaseChoose = pleaseChoose;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setParamCode(String paramCode) {
		this.paramCode = paramCode;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public void setExceptValue(String exceptValue) {
		this.exceptValue = exceptValue;
	}

	public void setMultiSelect(String multiSelect) {
		this.multiSelect = multiSelect;
	}

	public void setRequired(String required) {
		this.required = required;
	}
}
